package pri.damai.xiaowu.usercenter.sysauth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import pri.damai.xiaowu.common.core.tree.TreeUtils;
import pri.damai.xiaowu.usercenter.sysauth.command.UserResourceQuery;
import pri.damai.xiaowu.usercenter.sysauth.constant.ResourceConst;
import pri.damai.xiaowu.usercenter.sysauth.covert.ResourceConverter;
import pri.damai.xiaowu.usercenter.sysauth.entity.SysResource;
import pri.damai.xiaowu.usercenter.sysauth.entity.SysRoleResource;
import pri.damai.xiaowu.usercenter.sysauth.entity.SysUserRole;
import pri.damai.xiaowu.usercenter.sysauth.repository.SysResourceDao;
import pri.damai.xiaowu.usercenter.sysauth.service.SysResourceService;
import pri.damai.xiaowu.usercenter.vo.ResourceVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统资源表(SysResource)表服务实现类
 * @Desc
 * @Author DaMai
 * @Date 2022-03-03 17:00:31
 */
@Service("sysResourceService")
public class SysResourceServiceImpl extends ServiceImpl<SysResourceDao, SysResource> implements SysResourceService {

    @Resource
    SysUserRoleServiceImpl userRoleService;
    @Resource
    SysRoleResourceServiceImpl roleResourceService;


    public LambdaQueryWrapper<SysResource> getLambda() {
        return new LambdaQueryWrapper<>();
    }

    /**
     * 获取系统的资源列表
     * @apiNote [附加描述]
     * @param sysId 系统id
     * @return java.util.List<pri.damai.xiaowu.usercenter.vo.ResourceVO>
     * @author DaMai
     * @date 2022/3/15 21:54
     */
    public List<ResourceVO> getAllMenusBySysId(Long sysId) {

        LambdaQueryWrapper<SysResource> wrapper = getLambda().eq(SysResource::getSysId, sysId);

        List<SysResource> resources = this.list(wrapper);

        List<ResourceVO> voList = ResourceConverter.covertToResourceVO(resources);

        voList = voList.stream().filter(s -> !Objects.equals(s.getParentId(), ResourceConst.TOP_PATENT_ID))
                .collect(Collectors.toList());

        return TreeUtils.toTree(voList, sysId);
    }

    /**
     * 获取用户在某系统下拥有的资源
     * @apiNote [附加描述]
     * @param query 系统 id
     * @return java.util.List<pri.damai.xiaowu.usercenter.vo.ResourceVO>
     * @author DaMai
     * @date 2022/3/15 21:55
     */
    @Override
    public List<ResourceVO> getResourceBySysIdAndUserId(UserResourceQuery query) {

        // 获取用户拥有的资源
        LambdaQueryWrapper<SysUserRole> wrapper = userRoleService.getLambda()
                .eq(SysUserRole::getUserId, query.getUserId());
        Set<Integer> roleId = userRoleService.list(wrapper)
                .stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toSet());

        LambdaQueryWrapper<SysRoleResource> roleResourceWrapper = roleResourceService.getLambda()
                .in(CollectionUtils.isNotEmpty(roleId), SysRoleResource::getRoleId, roleId);
        List<Integer> resourceIds = roleResourceService.list(roleResourceWrapper)
                .stream()
                .map(SysRoleResource::getResourceId)
                .collect(Collectors.toList());

        List<SysResource> sysResources = this.listByIds(resourceIds);

        // 整理成资源树
        List<ResourceVO> voList = sysResources.stream()
                .filter(sysResource -> Objects.equals(sysResource.getSysId(), query.getSysId()))
                .map(sysResource -> {
                    ResourceVO resourceVO = new ResourceVO();
                    BeanUtils.copyProperties(sysResource, resourceVO);
                    return resourceVO;
                }).collect(Collectors.toList());

        return TreeUtils.toTree(voList, query.getSysId());
    }



}
